package com.jnu.myAccount.record;

import android.content.Context;

import com.jnu.myAccount.activity.MainActivity;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class FundAdapter implements Serializable {
    private String account;
    private String date="";
    private List<Record> recordList = null;

    public FundAdapter(String account, String date) {
        this.account = account;
        this.date = date;
    }

    public String getFileName()
    {
        return account+"#"+date;
    }

    public String getAccount() {
        return account;
    }

    public String getDate() {
        return date;
    }

    public List<Record> getRecordList() {
        return recordList;
    }

    //保存数据到本地文件中
    public void saveData()
    {
        try {
            ObjectOutputStream out = new ObjectOutputStream(
                    MainActivity.ContextInstance().openFileOutput(account+"#"+date, Context.MODE_PRIVATE));
            out.writeObject(recordList);
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //添加新流水记录
    public void addNewFundFlowInfo(Record info) {
        if (recordList == null) {
            recordList = new ArrayList<>();
        }
        recordList.add(info);
        saveData();
    }

    public boolean removeFundFlowInfo(Record record)
    {
        if(recordList == null || recordList.size() == 0)
            return true;
        recordList.remove(record);
        if(recordList.size()==0)
        {
            MainActivity.ContextInstance().getFileStreamPath(getFileName()).delete();
            return true;
        }else {
            DataPackage.Instance().saveFundAdapter(this);
            return false;
        }
    }
}
